package com.hrhx.duhongming.adapter.echarts;

import java.util.List;
import java.util.Map;

import com.github.abel533.echarts.axis.CategoryAxis;
import com.github.abel533.echarts.axis.ValueAxis;
import com.github.abel533.echarts.code.AxisType;
import com.github.abel533.echarts.code.Position;
import com.github.abel533.echarts.code.SeriesType;
import com.github.abel533.echarts.json.GsonOption;
import com.github.abel533.echarts.series.Line;
import com.hrhx.duhongming.adapter.EchartsAdapter;
import com.hrhx.duhongming.bean.EchartsConfig;

public class EchartsReverseBarAdapter implements EchartsAdapter{
	
	public static final EchartsReverseBarAdapter me = new EchartsReverseBarAdapter();
	
	//反转bar的适配器入口
	public GsonOption adapter(EchartsConfig echartsConfig,
			GsonOption gsonOption,
			List<Map<String,Object>> data){
		//EchartsReverseBarAdapter barAdapter = new EchartsReverseBarAdapter();
		gsonOption = me.legendAdapter(gsonOption,data);
		gsonOption = me.xAxisAdapter(echartsConfig,gsonOption,data);
		gsonOption = me.yAxisAdapter(echartsConfig,gsonOption,data);
		gsonOption = me.seriesAdapter(echartsConfig,gsonOption,data);
		return gsonOption;
	}
	
	//legend
	public GsonOption legendAdapter(GsonOption gsonOption,List<Map<String,Object>> data){
		if(data != null && data.size() != 0){
			//获取第一个List中的Map中的值
			for (String key : data.get(0).keySet()) {
				String[] k = key.split(":");
				//图例组件：Y轴数据类型
				if(k[0].startsWith("y")){
					gsonOption.legend().data(k[1]);
				}
			}
		}
		return gsonOption;
	}
	
	//x轴配置以及单位配置
	public GsonOption xAxisAdapter(EchartsConfig echartsConfig,GsonOption gsonOption,List<Map<String,Object>> data){
		ValueAxis valueAxis = new ValueAxis();
		valueAxis.name(echartsConfig.getXaxisName());
		valueAxis.type(AxisType.value);
		gsonOption.xAxis(valueAxis);
		return gsonOption;
	}
	
	//Y轴单位配置
	public GsonOption yAxisAdapter(EchartsConfig echartsConfig,GsonOption gsonOption,List<Map<String,Object>> data){
		CategoryAxis categoryAxis = new CategoryAxis();
		categoryAxis.name(echartsConfig.getYaxisName());
		// Y轴数据封装并解析
		if(data != null && data.size() != 0){
			for (Map<String,Object> map : data) {
				//遍历Map的有关x:deX轴数据
				for (String key : map.keySet()) {
					if(key.startsWith("x:")){
						categoryAxis.type(AxisType.category).data(map.get(key));
					}
				}
			}
		}
		gsonOption.yAxis(categoryAxis);
		return gsonOption;
	}
	
	//Y轴数据处理
	public GsonOption seriesAdapter(EchartsConfig echartsConfig,GsonOption gsonOption,List<Map<String,Object>> data){
		for(Object legendData : gsonOption.legend().data()){
			Line line = new Line();
			if(echartsConfig.getIsNeedStack()){				
				line.stack("总量").label().normal().show(true).position(Position.insideRight);
			}
			//echarts-type图表类型
			if("line".equalsIgnoreCase(echartsConfig.getType())){			
				line = line.type(SeriesType.line).name((String)legendData);
			}
			if("bar".equalsIgnoreCase(echartsConfig.getType())){
				line = line.type(SeriesType.bar).name((String)legendData);
			}			
			if(data != null && data.size() != 0){
				for (Map<String,Object> map : data) {
					//每个图表的Y轴数据匹配
					for (String key : map.keySet()) {
						if(key.endsWith((String)legendData)){
							line.data(map.get(key));
						}
					}
				}
			}
			gsonOption.series(line);
		}
		return gsonOption;
	}
}
